package com.gr100.biz.api.user;


import com.gr100.biz.api.bo.MetaObject;
import com.gr100.biz.api.user.hex.ValidateHex;
import com.jfinal.log.Log;
import com.mysql.jdbc.StringUtils;

import java.util.List;

/**
 * Created by james on 16/3/16.
 */
public class User {
    private static final Log logger = Log.getLog(User.class);

    public enum LEVEL{
        //2:普通会员，3:银卡会员，4:金卡会员，5:钻石会员，1:终身vip
        NORMAL ("2"),SILVER ("3"),GOLD ("4"),DIAMONDS ("5"),VIP ("1");

        private String _level;
        LEVEL(String level){
            _level = level;
        }

        public boolean equal(String level){
            return this._level.equalsIgnoreCase(level);
        }

        public String getValue(){
            return this._level;
        }

    }

    public enum LOGIN_TYPE{
        //1:手机号登录，2:email登录，3:微信号登录，4:qq号登录，5:用户名登录
        PHONE("1"),EMAIL("2"),WX ("3"),QQ("4"),USERNAME("5");

        private String _type;
        LOGIN_TYPE(String type){
            _type = type;
        }

        public boolean equal(String type){
            return this._type.equalsIgnoreCase(type);
        }

    }


    private String _type,_loginName,_loginPass,_cfgPass,_unionID;

    public User(String type, String loginName, String loginPass, String cfgPass,String unionID) {
        this._type = type;
        this._loginName = loginName;
        this._loginPass = loginPass;
        this._cfgPass = cfgPass;
        this._unionID = unionID;
    }

    public User setType(String type){
        this._type = type;
        return this;
    }


    public User setLoginName(String loginName){
        this._loginName = loginName;
        return this;
    }


    public User setLoginPass(String loginPass){
        this._loginPass = loginPass;
        return this;
    }

    public String getType() {
        return _type;
    }

    public String getLoginName() {
        return _loginName;
    }

    public String getLoginPass() {
        return _loginPass;
    }

    public String getUnionID() {
        return _unionID;
    }

    /**
     * 非空验证
     * @return
     */
    public void validate() throws ValidateHex {

        if(StringUtils.isNullOrEmpty(_loginName)){
            throw new ValidateHex(-601,"LOGINNAME CANT BE EMPTY","登录名不能为空");
        }
        logger.info(_loginName);
        logger.info(_loginName.length()+"");
        if("3".equals(_type) && _loginName.length() < 10){
            throw new ValidateHex(-610,"Openid value length exception","openid异常");
        }
        if(StringUtils.isNullOrEmpty(_type)){
            throw new ValidateHex(-602,"LOGINTYPE CANT BE EMPTY","登录类型不能为空");
        }
        if(!LOGIN_TYPE.PHONE.equal(_type) && !LOGIN_TYPE.EMAIL.equal(_type) && !LOGIN_TYPE.WX.equal(_type) && !LOGIN_TYPE.QQ.equal(_type) && !LOGIN_TYPE.USERNAME.equal(_type)){
            throw new ValidateHex(-603,"LOGINTYPE ABNORMAL VALUE","登录类型值异常");
        }
        if(!LOGIN_TYPE.WX.equal(_type) && !LOGIN_TYPE.QQ.equal(_type) && StringUtils.isNullOrEmpty(_loginPass)){
            throw new ValidateHex(-604,"LOGINPASS CANT BE EMPTY","密码不能为空");
        }
        if(!LOGIN_TYPE.WX.equal(_type) && !LOGIN_TYPE.QQ.equal(_type) && _loginPass.length()<6){
            throw new ValidateHex(-605,"LOGINPASS LENGTH CANNOT BE LESS THAN 6","密码长度不少于6位");
        }
        if(!LOGIN_TYPE.WX.equal(_type) && !LOGIN_TYPE.QQ.equal(_type) && !_loginPass.equals(_cfgPass)){
            throw new ValidateHex(-606,"LOGINPASS IS NOT CONSISTENT","密码不一致");
        }
    }


    /**
     * 验证用户是否已经存在
     * @return
     */
    public boolean checkExists(){
        return UserModel.me().checkExists(this);
    };

    /**
     * 保存用户信息
     * @return
     */
    public int signIn(){
        return UserModel.me().save(this);
    }

    /**
     * 用户查询
     * @return
     */
    public MetaObject signin(){
        return UserModel.me().find(this);
    }
}
